package com.nananer7.dao;

import com.nananer7.DBAccess;

import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    private int cid;
    public int getIid() {
        return iid;
    }

    public void setIid(int iid) {
        this.iid = iid;
    }

    private int iid;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    private int uid;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    private String content;

    public Timestamp getTime() {
        return time;
    }

    public void setTime(Timestamp time) {
        this.time = time;
    }

    private Timestamp time;

    //无参构造函数
    public CommentDao(){

    }

    //添加新的idea
    public boolean addComment(int uid,int iid,String content){
        Connection con =null;
        PreparedStatement pstmt =null;
        try {
            con = DBAccess.getConnection();
            String sql = "INSERT INTO comment(uid,iid,content) VALUES(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1,uid);
            pstmt.setInt(2, iid);
            pstmt.setString(3, content);

            if(pstmt.executeUpdate()>=1){
                return true;
            }else{
                return false;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt!=null)
                    pstmt.close();
                if(con!=null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //根据iid查找评论
    public List<CommentDao> findCommentsByIid(int iid){

        List list = new ArrayList<CommentDao>();
        Connection con =null;
        PreparedStatement pstmt =null;
        ResultSet rs = null;

        try {

            con = DBAccess.getConnection();
            String sql="select * from comment where iid=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1,iid);
            rs=pstmt.executeQuery();

            while(rs.next()){

                CommentDao commentDao = new CommentDao();
                commentDao.setCid(rs.getInt(1));
                commentDao.setUid(rs.getInt(2));
                commentDao.setIid(rs.getInt(3));
                commentDao.setContent(rs.getString(4));
                commentDao.setTime(rs.getTimestamp(5));

                list.add(commentDao);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    //单独测试使用
  /*  public static void main(String[] args) {

        *//*boolean bl=new CommentDao().addComment(2,17,"如果真的有记忆面包，那考试还有什么意义，不如直接比谁吃的多");
        System.out.println(bl);*//*

      *//*  CommentDao commentDao= new CommentDao();
        List<CommentDao> commentDaos= commentDao.findCommentsByIid(17);

        for(int i=0;i<commentDaos.size();i++){
            int iid= commentDaos.get(i).getIid();
            int uid=commentDaos.get(i).getUid();
            String content = commentDaos.get(i).getContent();
            Timestamp time = commentDaos.get(i).getTime();

            System.out.println(iid+" "+uid+" "+content+" "+time);
        }*//*
    }*/
}
